package cn.sc.allinpay.allfavour.business.cashiertrsdetailonline;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;

import com.alibaba.fastjson.JSONObject;

import java.math.BigDecimal;

import cn.pai.common.listener.OnPromptListener;
import cn.pai.mvp.callback.MpCallBack;
import cn.pai.mvp.intervenor.Intervenor;
import cn.pai.mvp.presenter.PaiPresenter;
import cn.sc.allinpay.allfavour.app.Constant;
import cn.sc.allinpay.allfavour.business.common.data.OilRecord;
import cn.sc.allinpay.allfavour.module.device.DeviceLogin;
import cn.sc.allinpay.allfavour.module.printer.IPrinter;
import cn.sc.allinpay.allfavour.module.printer.PrintData;
import cn.sc.allinpay.allfavour.module.printer.PrintManager;

/**
 * 交易交易详情 presenter
 * author：pany
 * on 2017/12/1 11:27
 */
public class CashierTransDetailOnlinePresenter extends PaiPresenter<CashierTransDetailOnlineContract.View> implements CashierTransDetailOnlineContract.Presenter {

    private CashierTransDetailOnlineModel model = new CashierTransDetailOnlineModel();

    private OilRecord transRecord;//交易记录
    private String payNum; //订单号
    private String oilName;
    private IPrinter printer;



    @Override
    protected Intervenor createIntervenor() {

        return new Intervenor() {
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                Bundle bundle = view.getBundle();
                payNum = bundle.getString(Constant.BundleKey.TRANS_ONLINE_RECORD);


            }

            @Override
            public void onResume() {
                super.onResume();
                DeviceLogin.login(view.getContext());
            }

            @Override
            public void onPause() {
                super.onPause();
                DeviceLogin.logout();
            }
        };
    }

    @Override
    public void start() {
        view.showDetailView(View.GONE);
        reqQueryOilDetail();
    }

    /**
     * 初始化视图
     */
    private void initView() {
        if (transRecord.getOilType().equals("01")) {
            oilName = "92#";
        } else if (transRecord.getOilType().equals("02")) {
            oilName = "95#";
        } else if (transRecord.getOilType().equals("03")) {
            oilName = "98#";
        } else if (transRecord.getOilType().equals("04")) {
            oilName = "0#";
        } else if (transRecord.getOilType().equals("05")) {
            oilName = "CNG";
        } else if (transRecord.getOilType().equals("06")) {
            oilName = "LNG";
        } else if (transRecord.getOilType().equals("07")) {
            oilName = "非油品";
        } else if (transRecord.getOilType().equals("08")) {
            oilName = "其它";
        }
        view.showOrderId(transRecord.getPayNum());//订单号
        view.showTransTime(transRecord.getPayTime());//交易时间
        view.showCashierName(oilName);
        view.showDisState(transRecord.getRightsAmt() != null ? "有抵扣": "无抵扣");
        view.showPosAmt(transRecord.getActulAmt().divide(new BigDecimal(100),2, BigDecimal.ROUND_DOWN).toString()); // 线上支付金额
        view.showTransAmt(transRecord.getTotalAmt().divide(new BigDecimal(100),2, BigDecimal.ROUND_DOWN).toString());//交易金额
        view.showGuideName(transRecord.getVehicleLicense() != null ? transRecord.getVehicleLicense() : "无");
        view.showMemberNo(transRecord.getMemCard() != null ? transRecord.getMemCard() : "无");
        view.showDiscount(transRecord.getRightsAmt() != null ? transRecord.getRightsAmt().divide(new BigDecimal(100),2, BigDecimal.ROUND_DOWN).toString() : "0.00");
        view.showCoupons(transRecord.getCouponAmt() != null ? transRecord.getCouponAmt().divide(new BigDecimal(100),2, BigDecimal.ROUND_DOWN).toString() : "0.00");
        view.showEWalletAmt(transRecord.getBalanceAmt() != null ? transRecord.getBalanceAmt().divide(new BigDecimal(100),2, BigDecimal.ROUND_DOWN).toString() : "0.00");
    }



    @Override
    public void callPrint() {
        printFull();
    }

    /**
     * 全卡券支付打印
     */
    public void printFull() {
        //因为onActivityResult回来没有立即执行onResume方法
        DeviceLogin.login(view.getContext());
        view.loading("正在打印...");
        PrintData data = new PrintData();
        data.setOrderId(transRecord.getPayNum());//订单号
        data.setOilName(oilName);
        data.setAmt(transRecord.getTotalAmt().divide(new BigDecimal(100),2, BigDecimal.ROUND_DOWN).toString());//订单总金额
        data.setTime(transRecord.getPayTime());
        data.setTrsAmt(transRecord.getScanAmt().divide(new BigDecimal(100),2, BigDecimal.ROUND_DOWN).toString());
        data.setDisRightsAmt(transRecord.getRightsAmt().divide(new BigDecimal(100),2, BigDecimal.ROUND_DOWN).toString());
        data.setDisCouponAmt(transRecord.getCouponAmt().divide(new BigDecimal(100),2, BigDecimal.ROUND_DOWN).toString());
        data.setDisEWalletAmt(transRecord.getBalanceAmt().divide(new BigDecimal(100),2, BigDecimal.ROUND_DOWN).toString());
        if (transRecord.getPrintReceipt() != null) {
            data.setPrintCount("1");
        }
        printer = PrintManager.createPrinter(PrintManager.PRINT_OIL, data);
        printer.print(new IPrinter.OnPrintListener() {
            @Override
            public void onStart(int step) {

            }

            @Override
            public void onSuccess(int step) {
                if (view != null) {
                    view.loaded();
                    view.toast("打印成功");
                    transRecord.setPrintReceipt(1);
                    view.loading("加载中...");
                    model.reqQueryOilUpdate(payNum, new MpCallBack<JSONObject, String>() {
                        @Override
                        public void onSuccess(JSONObject resp) {
                            view.loaded();
                        }
                        @Override
                        public void onFailure(String response, String message) {
                            view.loaded();
                            view.prompt(message);
                        }
                    });
                }
            }

            @Override
            public void onFail(int code, String msg) {
                view.loaded();
                view.prompt(false, msg + ",是否继续打印", new OnPromptListener("确定", "取消") {
                    @Override
                    public void onPositive(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        printFull();
                    }

                    @Override
                    public void onNegative(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        view.setResult(Activity.RESULT_OK, null);
                        view.finish();
                    }
                });
            }
        });
    }

    @Override
    public void reqQueryOilDetail() {
            view.loading("数据查询中...");
            model.reqQueryOilDetail(payNum, new MpCallBack<OilRecord, String>() {
                @Override
                public void onSuccess(OilRecord resp) {
                    view.showDetailView(View.VISIBLE);
                    transRecord = resp;
                    initView();
                    view.loaded();

                }

                @Override
                public void onFailure(String response, String message) {
                    view.loaded();
                    view.prompt(message);
                }
            });

    }
}
